function OUT_Bits = HF_Descrambling (IN_Frame,Rate)

%*------------------------------ HF_Descrambling --------------------------------------%
%
% HF_Descrambling
%
% The fuction that de-scrambling the input frame dependent on its rate, and
% hence the length of output frame length must match with the corressponding
% rate as follows:
%
%
%           Rate            |           Input Frame Length
%
%           3200           |                    512
%           4800           |                    768
%           6400           |                    1024
%           8000           |                    1280
%           9600           |                    1536
%
%
%*------------------------------------------------------------------------------------%

LUT_Rate_Symbols = [2 3 4 5 6 6]; %No of symbols corressponding to each rate.
Data_Rates = [3200 4800 6400 8000 9600 12800];
Rate_Index = find(Data_Rates==Rate);


% LFSR INITIALIZATION by one
LFSR_Register = [0 0 0 0 0 0 0 0 1];
N_Symbols = LUT_Rate_Symbols(Rate_Index); % No of Symbols for the current rate


for index = 1:256

    if (Rate == 3200 || Rate == 4800) %case of QPSK and 8PSK scrambling
        LFSR_Int =bi2de(fliplr((LFSR_Register(9-N_Symbols+1:9))));
        %transform the output of the LFSR to integer format
        OUT_Symbols(index) = (IN_Frame(index)-LFSR_Int);
        if OUT_Symbols(index) < 0
            OUT_Symbols(index) = OUT_Symbols(index)+8;
        end
        OUT_Bits_Matrix(index,:) = Symbol_Demapping (OUT_Symbols(index),Rate,0);
        
        

    else  % case of all except Q/8PSK
        Data_Bits(index,:) = Symbol_Demapping (IN_Frame(index),Rate,0); 
        OUT_Bits_Matrix(index,:) = bitxor(Data_Bits(index,:),fliplr(LFSR_Register(9-N_Symbols+1:9)));
        %xor with the equivalent symbol bit sequence
    end
    
    % LFSR OPERATION
    for lfsr_index = 1:N_Symbols  %shifting by the number of symbols
        LFSR_Calc_Value = bitxor (LFSR_Register(9),LFSR_Register(5)) ;
        %Actually we are xoring the LFSR_Register(1) and LFSR_Register(5)
        %and the out in LFSR_Register(9), but the equation reversed for the
        %reason of LFSR_Register vector arrangement.
        LFSR_Register(2:9) = LFSR_Register (1:8);
        %shift one bit
        LFSR_Register(1) = LFSR_Calc_Value;
    end
    
end

OUT_Bits = reshape(OUT_Bits_Matrix.',1,N_Symbols*length(OUT_Bits_Matrix));

